<?php

namespace App\Http\Controllers\API;

use App\Models\Order;
use App\Services\ExpiredService;

class OrdersController extends Controller
{
    public function store()
    {
        $order_id = request('orderid');
        $price = request('price');
        $user_id = request('orderuid');
        $key = request('key');
        $real_price = request('realprice');

        if ($order_id && $price && $user_id && $key && $real_price) {
            $order = Order::where('order_id', $order_id)
                ->where('price', $price)
                ->where('user_id', $user_id)
                ->where('key', $key)
                ->first();
            if ($order) {
                $order->update([
                    'real_price' => $real_price,
                    'status' => 1,
                ]);

                $expired_service = new ExpiredService($user_id);
                switch ($price) {
                    case '9.9':
                        $days = 1;
                        break;
                    case '39.9':
                        $days = 30;
                        break;
                    case '99':
                        $days = 365;
                        break;
                    default:
                        $days = 0;
                }
                $expired_service->addDays($days);
            }
        }
    }
}
